package graphic;

import java.util.HashMap;
import java.util.Iterator;

import javax.swing.JTabbedPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import core.Client;
import core.Program;

public class FilesTabbedPanel extends JTabbedPane {

	protected static FilesTabbedPanel FILESPANEL;
	
	HashMap<String, ProgramTab> tabs;
	
	String selectedTab;
	
	public FilesTabbedPanel() {
	
		this.tabs = new HashMap<String, ProgramTab>();
		
		HashMap<String, Program> programs = Client.getInstance().getAssociatedPrograms();
		
		String programName;
		ProgramTab programTab;
		for (Iterator iterator = programs.keySet().iterator(); iterator.hasNext();)
		{
			programName = (String) iterator.next();		
			programTab = new ProgramTab(programName);
		
			if(this.selectedTab == null) selectedTab = programName;
			this.tabs.put(programName, programTab);
			super.addTab(programName, programTab);
		}
		
		super.addChangeListener(new ChangeListener() {
			
			@Override
			public void stateChanged(ChangeEvent e)
			{
			    JTabbedPane tabSource = (JTabbedPane) e.getSource();
			    FilesTabbedPanel.this.selectedTab = tabSource.getTitleAt(tabSource.getSelectedIndex());	
			    ReleasesPanel.getInstance().createTable();
			    ReleasesPanel.getInstance().setFileId(-1);
			}
		});
	}
	
	public HashMap<String, ProgramTab> getTabs() {
		return tabs;
	}

	public String getSelectedProgramName() {
		return selectedTab;
	}

	public ProgramTab getSelectedTab()
	{
		return this.tabs.get(this.getSelectedProgramName());
	}
	
	public static FilesTabbedPanel getIstance()
	{
		if(FILESPANEL == null) FILESPANEL = new FilesTabbedPanel();
		return FILESPANEL;
	}
}
